package cn.com.jsepc.rep.comm;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import com.jsepc.jhtj.cps.project.dao.StoreLoadDAO;
//import com.jsepc.jhtj.ims.MQSender;
//import com.jsepc.jhtj.ims.SysListener;
//import com.jsepc.jhtj.user.UserDAO;
//import com.jsepc.jhtj.user.vo.UserVO;

public class ServiceLogin extends HttpServlet {

//	private String authPath;
	private String jhtjVersion;
    private ServletConfig config=null;
	/**
	 * Constructor of the object.
	 */
	public ServiceLogin() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String key ="";
		String authPath = "";
		Properties prop = new Properties();
//		String[] str = new UnityCode().getBianmxx(ConstVar.AUTHPATH);
//	    if (str!=null && str.length>1){
//	    	key = str[0];
//			authPath = str[1];
//	    }
//		key = new UnityCode().getBianmz(ConstVar.AUTHPATH);
//		if(key==null||key.equals("")){
//			key=config.getInitParameter("Key");
//			key = "C74C9E687632232F";
//		}
		try{
			com.jsepc.portal.sso.DES.setKey(key);
		}catch(Exception ex){
			System.out.println(ex.getLocalizedMessage());
		}finally{
			prop = null;
		}

		response.setContentType("text/html;charset=UTF-8");
//		StoreLoadDAO sdao = StoreLoadDAO.getInstance();
//		if (sdao.isNull()) {
//			sdao.setAllHashtable();
//		}
		PrintWriter out = response.getWriter();
		String testuid = request.getParameter("testuid");
		String value = null;
		if (testuid == null || testuid.equals("")) {
			try {
				//value = com.jsepc.portal.sso.Helper.generateUserCode(request);
				value = com.jsepc.portal.sso.Helper.newGenerateUserCode(request);
			} catch (Exception ex) {
				value = null;
			}

		} else {
			value = "111";
		}

		String pagename = "index.jsp";
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		java.util.Date createDate = Calendar.getInstance().getTime();

		String userAddr = request.getRemoteAddr();
		String userAddrs = request.getServerName();
		String sgComp = "";
		String sgUserCode = "";
		String codingUnits = "";
		String strAreaCode = "";

		HttpSession session = request.getSession(true);

		if (session.getAttribute("JHTJ_UserCode") != null) {
			response.sendRedirect(pagename);
		} else {
			if (pagename == null) {
				pagename = "index.jsp";
			}
			if (value == null) {
				// 进入此处，说明肯定是省公司用户，非国网用户
				response.sendRedirect(authPath
						+ URLEncoder.encode(request.getRequestURL().toString())
						+ "&pagename=" + pagename);
			} else {
				String userCode = "";
				if (testuid != null && !testuid.equals("")) {
					userCode = testuid;
				} else {
					try {
//						userCode = com.jsepc.portal.sso.Helper
//								.generateUserCode(request);
						userCode = com.jsepc.portal.sso.Helper.newGenerateUserCode(request);
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
//				UserDAO userDao = UserDAO.getInstance();
//				UserVO userVO = new UserVO();
//				ConstUtil.ORG=config.getInitParameter(userAddrs+"_Org");
//				userVO = userDao.getAUserVO(userCode);
//				if (userVO.isNewUser()) {
//					System.out.println("[" + formatter.format(createDate)
//							+ "] " + userAddr + " Use UserCode : " + userCode
//							+ " Can't Login. eg : Not Enough Popedom user!");
//					response.sendRedirect("tips.jsp");
//				} else {
//					int iCount = userDao.getUserMenuCount(userCode);
//					if (iCount <= 0) {
//						System.out
//								.println("["
//										+ formatter.format(createDate)
//										+ "] "
//										+ userAddr
//										+ " Use UserCode : "
//										+ userCode
//										+ " Can't Login. eg : Not Enough Popedom menu!");
//						response.sendRedirect("tips.jsp");
//					} else {
//						userDao.setLoginUser(userCode);
//						SysListener.onLineUsers.put(userCode, userCode);
//						SysListener.setOnlineUser(SysListener.onLineUsers.keySet().size());
//						SystemMenu obj = new SystemMenu();
//						strAreaCode = obj.getAreaByUserCompCode(userVO
//								.getUserCompCode());
//						if (strAreaCode.equals("-1")) {
//							//
//						}
//						session.setAttribute("JHTJ_UserCode", userCode);
//						session.setAttribute("JHTJ_Version", jhtjVersion);
//						session.setAttribute("JHTJ_UserName", userVO
//								.getUserName());
//						session.setAttribute("JHTJ_UserAddr", userAddr);
//						session.setAttribute("JHTJ_UserArea", strAreaCode);
//						session.setAttribute("JHTJ_DeptCode", userVO
//								.getUserCompCode());
//						session.setAttribute("JHTJ_DeptType", userVO
//								.getUserDeptType());
//						session.setAttribute("JHTJ_CompName", userVO
//								.getUserCompName());
//						session.setAttribute("JHTJ_DeptName", userVO
//								.getUserDeptName());
//						String sUserVolt = userDao.getUserVolts(userCode);
//						session.setAttribute("JHTJ_UserVolt", sUserVolt);
//						String sUserRole = userDao.getUserRoles(userCode);
//						session.setAttribute("JHTJ_UserRole", sUserRole);
//						System.out
//								.println("[" + formatter.format(createDate)
//										+ "] " + userAddr + " Use UserCode : "
//										+ userCode + " Login " + pagename
//										+ " Succeed!");
//						request.getRequestDispatcher(pagename).forward(request,
//								response);
//						// response.sendRedirect(pagename);
//					}
//				}
			}
		}
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");

		PrintWriter out = response.getWriter();

		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	public void init(ServletConfig config) throws ServletException {
		// Put your code here
		// System.out.println("111");
		this.config=config;
//		authPath = config.getInitParameter("AuthPath");
		jhtjVersion = config.getInitParameter("Version");
	}

}
